<template>
  <el-container class="home-container">
    <!-- 头部区域-->
    <el-header>
      <div>
        <span>啾咕联盟后台管理</span>
      </div>
      <el-popover placement="bottom" trigger="hover" style="text-align:center;">
        <div>
          <li @click="yaoqing" class="popo">邀请</li>
          <div style="margin:5px 0;"></div>
          <li @click="logout" class="popo">退出</li>
        </div>
        <el-button size="mini" slot="reference">{{username}}</el-button>
      </el-popover>
    </el-header>
    <el-container>
      <!-- 侧边栏-->
      <el-aside :width="isCollapse ? '60px' : '180px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区域-->
            <template slot="title">
              <!-- 图标-->
              <i :class="iconsObj[item.id]" class="iconMenu"></i>
              <!-- 文本-->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单-->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveActivePath('/'+subItem.path)"
            >
              <template slot="title">
                <!-- 图标-->
                <i class="el-icon-menu"></i>
                <!-- 文本-->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体内容-->
      <el-main>
        <!-- 占位符-->
        <router-view :username="username"></router-view>
      </el-main>
    </el-container>
    <el-dialog title="生成邀请码" :visible.sync="inviteDialog" width="50%" class="add_dialog">
      <!--内容主体-->
      <el-input v-model="inviteCode"></el-input>
      <!--按钮-->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save()">使用</el-button>
        <el-button @click="reset()">重置</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      username: '',
      menuList: [
        {
          id: 1,
          authName: '用户管理',
          children: [
            {
              authName: '全部用户',
              path: 'suphome/userinfo'
            },
            {
              authName: '普通用户',
              path: 'suphome/userdetail'
            }
          ]
        },
        {
          id: 2,
          authName: '帖子管理',
          children: [
            {
              authName: '精华帖',
              path: 'suphome/creamPost'
            },
            {
              authName: '普通帖',
              path: 'suphome/ordinPost'
            }
          ]
        },
        {
          id: 3,
          authName: '消息管理',
          children: [
            {
              authName: '所有消息',
              path: 'suphome/allMsg'
            }
          ]
        },
        {
          id: 4,
          authName: '积分管理',
          children: [
            {
              authName: '积分排名',
              path: 'suphome/integ'
            }
          ]
        },
        {
          id: 5,
          authName: '版块管理',
          children: [
            {
              authName: '版块列表',
              path: 'suphome/college'
            }
          ]
        }
      ],
      iconsObj: {
        '1': 'iconfont icon-users',
        '0': 'iconfont icon-tijikongjian',
        '2': 'iconfont icon-shangpin',
        '3': 'iconfont icon-danju',
        '4': 'iconfont icon-baobiao',
        '5': 'iconfont icon-tijikongjian',
        '6': 'iconfont icon-showpassword'
      },
      isCollapse: false,
      activePath: '',
      inviteDialog: false,
      inviteCode: ''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前的路径
    saveActivePath(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    },
    // yaoqing
    yaoqing() {
      this.inviteDialog = true
      this.suiji()
    },
    // 邀请码重置
    reset() {
      this.suiji()
    },
    // 生成随机数
    suiji() {
      var invite = setInterval(() => {
        this.inviteCode = parseInt(
          Math.random() * (1000000 - 100000 + 1) + 100000,
          10
        )
      }, 100)
      setTimeout(() => {
        clearInterval(invite)
      }, 1000)
    },
    // 使用邀请码
    async save() {
      const res = await this.$http.post('/inviteInsert', {
        createDate: new Date(),
        userName: this.username,
        invitecode: this.inviteCode
      })
      if (res.meta.status !== 200) {
        return this.$Message.error('生成错误，请重试')
      }
      this.$Message.success('保存成功')
      this.inviteDialog = false
    }
  },
  created() {
    this.username = window.sessionStorage.getItem('login')
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  watch: {
    '$route.path': function(val) {
      // console.log(oval)
      if (val !== '/suphome/showPost' && val !== '/suphome/showMsg') {
        this.activePath = val
      }
      console.log(this.activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  .popo:hover {
    background-color: #4a5064;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
    .iconMenu {
      padding-right: 10px;
    }
  }
  .toggle-button {
    font-size: 10px;
    text-align: center;
    letter-spacing: 0.2em;
    line-height: 24px;
    color: #fff;
    background-color: #4a5064;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
</style>
